
$(function () {
    let id;
    layui.laydate.render({
        elem: '#articleDate', //指定元素
        theme: '#393D49'
    })

    tinymce.init({
        selector: '#articleContent',
        language: 'zh_CN',
        statusbar: false
    })
    function getCategory() {
        $.get('/admin/category/list', function (res) {
            const html = template('categoryId', { list: res.data })
            $("[name='categoryId']").html(html)
        })
    }
    function getArticleContent() {
        const ups = new URLSearchParams(this.location.search)
        id = ups.get('id')
        $.get('/admin/article/search', { id }, function (res) {
            const { categoryId, content, cover, date, title } = res.data
            $("[name='title']").val(title)
            $('#coverimg').prop('src', cover)
            $("[name='date']").val(date)
            $("[name='categoryId']").val(categoryId)
            $('#articleContent').val(content)
        })
    }
    getCategory()
    getArticleContent()
    function saveArticle(type) {
        const fd = new FormData($('#form')[0])
        const content = tinyMCE.editors['articleContent'].getContent()
        fd.append('content', content)
        fd.append('id', id)
        if (type === '已发布') {
            fd.append('state', '已发布')
        } else {
            fd.append('state', '')
        }
        $.ajax({
            url: '/admin/article/edit',
            data: fd,
            type: 'post',
            contentType: false,
            processData: false,
            success(res) {
                if (res.code === 200) {
                    location.href = 'article_list.html'
                }
            }
        })
    }
    window.saveArticle = saveArticle
})